import apis from '@/api/apis'


export default {
	namespaced: true,
	state: {
		fd: [], //基金首页数据
		ftd: [], //基金排行“全部”数据
		fdd: {}, //基金详情数据
		fmd: [], //货币基金数据
		fed: [], //指数基金数据
		fhd: [], //热门基金数据
		fpd: [], //基金持仓数据
		fgd: {}, //基金折线图数据
		hsd: [] //搜索历史数据

	},
	mutations: {

		// 基金首页数据赋值
		setAllFund(state, newData) {
			state.fd = [...newData]
			// console.log(state.fd)
		},

		// 基金排行数据赋值	
		setFundTop(state, newData) {
			state.ftd = [...state.ftd, ...newData]
			// console.log(state.ftd, 'top数据')
		},

		// 基金详情数据赋值
		setFundDetail(state, newData) {
			state.fdd = {
				...newData
			}
		},

		// 货币基金数据赋值
		setFundMoney(state, newData) {
			state.fmd = newData
			// console.log(state.fmd,'set')
		},

		// 指数基金数据赋值
		setFundExponent(state, newData) {
			state.fed = [...state.fed, ...newData]
			// console.log(state.fed, '指数基金')
		},

		// 热销榜基金赋值
		setFundHot(state, newData) {
			state.fhd = [...newData]
			// console.log(state.fhd)
		},


		// 设置持仓数据赋值
		setgetPosition(state, newData) {
			state.fpd = newData

		},

		// 折线图数据赋值 
		setFundGraph(state, newData) {
			state.fgd = {...newData}
			console.log(state.fgd  ,'折线图数据')
			// console.log(Math.max(...newData.series.data))

		},
		
		// 历史搜索记录
		setHistorySearch(state, newData) {
			if (newData) {
				state.hsd.push(newData)
			}
			if (state.hsd.length > 10) {
				state.hsd = state.hsd.splice(0)
			}
		},

		// 清除折线数据
		clearFgd(state) {
			state.fgd = []
		},


	},
	actions: {
		// 首页基金数据
		async getAllFund(context) {
			const result = await apis.product.getAllFund()
			// 数据页面保存一致添加一下字段分为按照indexd奇偶来划分
			const arr = result.data.map((i, index) => {
				if (index % 2 == 0) {
					i.fundTag = "五星评级";
					i.fundPoster = "名匠坐镇";
					i.fundYear = "近一年收益率";
					i.fundSlogan = "稳健风格 前瞻配置";
				} else {
					i.fundTag = "人气债基";
					i.fundPoster = "熨平波动";
					i.fundYear = "近一年收益率";
					i.fundSlogan = "震荡优选 严控回撤";
				}
				return i
			})
			context.commit('setAllFund', arr)
		},


		// 基金排行数据处理函数
		async getFundTop(context, arr) {
			console.log(arr)
			if (arr[0] == '全部') {
				let result = await apis.product.getAllFund({
					pageNum: arr[1]
				})
				context.commit('setFundTop', result.data)
			} else {
				let result = await apis.product.getAllFund({
					type: arr[0],
					pageNum: arr[1]
				})
				context.commit('setFundTop', result.data)
			}
		},

		// 基金详情处理函数
		async getFundDet(context, code) {
			const result = await apis.product.getFundDetail(code)
			context.commit('setFundDetail', result.data)
		},

		// 折线图数据处理函数
		async getFundGraph(context, arr) {

			// 由于状态机中只能传两个参数，所以为了传多个数据将arr[code,pageNum]用数组的形式传过来，
			let arr1 = await apis.product.getGraph(arr[0])
			// 选了这只基金作为沪深300用来和上面那支基金作为对比
			let arr2 = await apis.product.getGraph({
				code: '000042'
			})

			if (context.state.fdd.name.indexOf('货币') == -1) {
				//将数据进行统一处理为秋云插件要求的数据格式
				arr1.data.oneMonth.series.push(...arr2.data.oneMonth.series)
				arr1.data.threeMonth.series.push(...arr2.data.threeMonth.series)
				arr1.data.sixMonth.series.push(...arr2.data.sixMonth.series)
				arr1.data.oneYear.series.push(...arr2.data.oneYear.series)
				arr1.data.threeYear.series.push(...arr2.data.threeYear.series)

			
				// 利用传过来的参数按类别进行分类赋值
				switch (arr[1]) {
					case 0:
						arr1.data.oneMonth.series[0].name = '本产品涨幅'
						arr1.data.oneMonth.series[1].name = '沪深300涨幅'
						context.commit('setFundGraph', arr1.data.oneMonth)
						break;
					case 1:
						arr1.data.threeMonth.series[0].name = '本产品涨幅'
						arr1.data.threeMonth.series[1].name = '沪深300涨幅'
						context.commit('setFundGraph', arr1.data.threeMonth)
						break;
					case 2:
						arr1.data.sixMonth.series[0].name = '本产品涨幅'
						arr1.data.sixMonth.series[1].name = '沪深300涨幅'
						context.commit('setFundGraph', arr1.data.sixMonth)
						break;
					case 3:
						arr1.data.oneYear.series[0].name = '本产品涨幅'
						arr1.data.oneYear.series[1].name = '沪深300涨幅'
						context.commit('setFundGraph', arr1.data.oneYear)
						break;
					case 4:
						arr1.data.threeYear.series[0].name = '本产品涨幅'
						arr1.data.threeYear.series[1].name = '沪深300涨幅'
						context.commit('setFundGraph', arr1.data.threeYear)
						break;
				}
			}

		},

		// 持仓处理函数****这个数据需要后端重新处理才能再做*****
		async getFundPt(context, code) {
			const result = await apis.product.getPosition(code)
			console.log(result, '详情')
			if (result.data) {
				const bond = result.data.bond.substr(0, result.data.bond.length - 1) - 0;
				const cash = result.data.cash.substr(0, result.data.cash.length - 1) - 0;
				const stock = result.data.stock.substr(0, result.data.stock.length - 1) - 0;
				const title = result.data.title.split(/[\s]/)
				const arr1 = result.data.stockList
				const arr2 = {
					series: [{
						data: [{
								name: "债券",
								value: bond
							},
							{
								name: "存款",
								value: cash
							},
							{
								name: "股票",
								value: stock
							}
						]
					}]
				}
				const arr = [title[title.length - 1], arr1, arr2]
				
				context.commit('setgetPosition', arr)
			}


		},


		// 货币基金处理函数-
		async getFundMoney(context) {
			const result = await apis.product.getFundCurrency()
			context.commit('setFundMoney', result.data)
			// console.log(result.data, '货币基金')
		},

		// 指数基金处理函数
		async getFundEt(context, pageNum) {
			// console.log(pageNum)
			let pageSize = 10 //每页多少条
			let finishedText = '' //加载完毕文本提示			
			const result = await apis.product.getFundExponent(pageNum)
			if (result.code == 200) {
				// console.log(result, '指数基金11')
				let a1 = {
					name: '热销选基',
					list: result.data.slice(0, parseInt(result.data.length / 2))
				}
				let a2 = {
					name: '指数选基',
					list: result.data.slice(parseInt(result.data.length / 2))
				}
				let arr = []
				arr.push(a1, a2, )
				// console.log(arr,'处理之后的指数基金')
				context.commit('setFundExponent', arr)
				if (result.data.length == 0) {
					uni.showToast({
						title: '暂无数据'
					})
					return
				} else if (result.data.length < pageSize) {
					uni.showToast({
						title: '没有更多了'
					})
					return
				}
			} else {
				return result.Msg
			}

		},

		// 热门基金处理函数
		async getFundht(context) {
			const result = await apis.product.getFundHot()
			context.commit('setFundHot', result.data)
		},
		
		// 搜索处理函数
		async getSearch(context, data) {
			const result = await apis.product.getSearchFund(data)
			return result.data
		}

	}
}
